package com.spzx.product.service.impl;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.domain.Brand;
import com.spzx.product.mapper.BrandMapper;
import com.spzx.product.service.IBrandService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 品牌Service业务层处理
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements IBrandService{

    private boolean isBrandExists(Brand brand) {
        Brand existingBrand = baseMapper.selectByName(brand.getName());
        return existingBrand != null;
    }
    @Override
    public int addBrand(Brand brand) {
        if (brand == null) {
            throw new IllegalArgumentException("品牌对象不能为空");
        }
        // 检查品牌是否存在
        if (isBrandExists(brand)) {
            throw new ServiceException("品牌已存在");
        }

        // 如果品牌不存在，则插入新品牌
        return baseMapper.insert(brand);
    }

    @Override
    public Page<Brand> getBrandList(Integer page, Integer size) {
        Page<Brand> brandPage = new Page<>(page, size);
        return baseMapper.getBrandList(brandPage);
    }
}
